home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / mm-0.90 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  8.6 KB  |  400 lines

  1. /*
  2.  * Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  * the City of New York.  Permission is granted to any individual or
  4.  * institution to use, copy, or redistribute this software so long as it
  5.  * is not sold for profit, provided this copyright notice is retained.
  6.  */
  7.  
  8. #ifndef lint
  9. static char *rcsid = "$Header: /f/src2/encore.bin/cucca/mm/tarring-it-up/RCS/misc.c,v 2.4 90/10/04 18:24:48 melissa Exp $";
  10. #endif
  11.  
  12. /*
  13.  * misc.c - miscellaneous commands
  14.  */
  15.  
  16. #include "mm.h"
  17. #include "parse.h"
  18. #include "help.h"
  19. #include "rd.h"
  20.  
  21. #ifdef HAVE_QUOTAS
  22. #   ifdef HAVE_QUOTACTL
  23. #    include <ufs/quota.h>
  24. #    include <mntent.h>
  25. #   else
  26. #    include <sys/quota.h>
  27. #   endif
  28. #endif
  29.  
  30.  
  31. int
  32. cmd_version (n)
  33. int n;
  34. {
  35.     confirm ();
  36.     printf ("%s\n", mm_version);
  37.     puts("Copyright (c) 1986, 1990\n\
  38. \tThe Trustees of Columbia University in the City of New York");
  39.     printf ("Compiled %s\n", mm_compiled);
  40.     printf ("Report bugs to %s\n", BUGSTO);
  41. }
  42.  
  43. #ifdef HAVE_QUOTAS
  44. #define ST_GOODQUOTA 0
  45. #define ST_BADQUOTA  1
  46. #define ST_NOQUOTA   2
  47. #endif
  48.  
  49. int
  50. cmd_status (n)
  51. int n;
  52. {
  53.     int verbose = FALSE;
  54. #ifdef HAVE_QUOTAS
  55.     int quotaflag = ST_GOODQUOTA;
  56.     struct dqblk qblk;
  57. #ifdef HAVE_QUOTACTL
  58.     char *bdev, *getspecialname();
  59. #endif
  60. #endif
  61.     struct stat sbuf;
  62.     static fdb conffdb = { _CMCFM, CM_SDH, NULL, NULL, 
  63.                    "confirm for status summary ", 
  64.                    NULL, NULL};
  65.     static keywrd ver_key[] = {
  66.         { "verbose", 0 , (keyval) 0 },
  67.     };
  68.     static keytab vertab = { sizeof (ver_key) / sizeof (keywrd),
  69.                   ver_key };
  70.     static fdb verswi = { _CMKEY, 0, NULL, (pdat) &vertab };
  71.  
  72.  
  73.     parse (fdbchn (&verswi, &conffdb, NULL), &pv, &used);
  74.     if (used == &verswi) {
  75.     confirm();
  76.     verbose = TRUE;
  77.     }
  78.  
  79.     show_route(TRUE);            /* show mail routing */
  80.  
  81.     if (cf) {
  82.     int i, nrec = 0, ndel = 0, nuns = 0;
  83.     for (i = 0; i < cf->count; i++)    {
  84.         if (cf->msgs[i+1].flags & M_DELETED)
  85.         ndel++;
  86.         if (cf->msgs[i+1].flags & M_RECENT)
  87.         nrec++;
  88.         if (!(cf->msgs[i+1].flags & M_SEEN))
  89.         nuns++;
  90.     }
  91.     printf (" File %s (%s%s%s)\n",cf->filename, msg_ops[cf->type].typename,
  92.         (cf->flags&(MF_DIRTY|MF_MODIFIED) ? ", modified" : ""),
  93.         (cf->flags&MF_RDONLY) ? ", read-only" : "");
  94.     printf (" %d messages, %d old, %d deleted, %d unseen, %dk Bytes\n",
  95.         cf->count, cf->count - nrec, ndel, nuns, (cf->size+512)/1024);
  96.     printf (" Currently at message %d\n", cf->current);
  97.     }
  98.     else
  99.     printf (" No current mail file\n");
  100.     if (!verbose)
  101.     return;                /* done */
  102. #ifdef HAVE_QUOTAS
  103.     if (cf == NULL)
  104.     quotaflag = ST_NOQUOTA;
  105.     else {
  106.     if (fstat (fileno(cf->filep), &sbuf) != 0) {
  107.         perror (cf->filename);
  108.         quotaflag = ST_BADQUOTA;
  109.     }
  110. #ifdef HAVE_QUOTACTL
  111.     else if ((bdev = getspecialname (sbuf.st_dev)) == NULL) {
  112.         fprintf (stderr, "%s: couldn't get block special device\n", 
  113.              cf->filename);
  114.         quotaflag = ST_BADQUOTA;
  115.     }
  116.     else if (quotactl (Q_GETQUOTA, bdev, UID, &qblk) != 0)
  117.         quotaflag = ST_NOQUOTA;    /* assume this, random error codes */
  118. #else
  119.     else if (quota (Q_GETDLIM, UID, (int) sbuf.st_dev, &qblk) != 0)
  120.         quotaflag = ST_NOQUOTA;    /* assume this, random error codes */
  121. #endif
  122.     if (quotaflag != ST_NOQUOTA) {
  123.         printf (" Blocks free: ");
  124.         if (quotaflag == ST_BADQUOTA)
  125.         printf ("unknown\n");
  126.         else {                /* ST_GOODQUOTA */
  127.         u_long i;
  128.         i = qblk.dqb_bhardlimit - qblk.dqb_curblocks;
  129.         printf ("%ld (%ld kb)\n", i, (dbtob(i)>>10)); /* 2^10 = 1 kb */
  130.         }
  131.     }
  132.     }
  133. #endif
  134.     printf (" User: %s <%s@%s>\n", 
  135.         real_personal_name ? real_personal_name : "",
  136.         user_name, fullhostname);
  137.     printf (" Process ID = %d, User ID = %d\n", PID, UID);
  138. }
  139.  
  140. #ifdef HAVE_QUOTAS
  141. #ifdef HAVE_QUOTACTL
  142. /*
  143.  * getspecialname:
  144.  * get the block special device name for this major/minor number
  145.  * cache the result, since they'll probably run stat on the same file
  146.  * over and over (or at least the same filesystem)
  147.  */
  148. char *
  149. getspecialname(filedev)
  150. dev_t filedev;
  151. {
  152.     FILE *mntf;
  153.     struct mntent *ent;
  154.     struct stat statb;
  155.     static dev_t oldfiledev = 0;
  156.     static char *oldspecialname = NULL;
  157.  
  158.     if (oldfiledev == filedev)        /* same partition as last time */
  159.     return (oldspecialname);
  160.  
  161.     if ((mntf = setmntent (MNTTAB, "r")) == NULL)
  162.     return (NULL);
  163.     while ((ent = getmntent(mntf)) != NULL) {
  164.     if (stat(ent->mnt_dir, &statb) != 0) /* look at device */
  165.         return (NULL);
  166.     if (statb.st_dev == filedev) {    /* the right raw device? */
  167.         endmntent(mntf);        /* be neat */
  168.         safe_free (oldspecialname);
  169.         if ((oldspecialname = malloc (strlen (ent->mnt_fsname) +1))
  170.         != NULL){
  171.         strcpy (oldspecialname, ent->mnt_fsname); /* cache it */
  172.         oldfiledev = filedev;
  173.         }
  174.         else
  175.         oldfiledev = 0;        /* flag no cached name */
  176.         return (ent->mnt_fsname);
  177.     }
  178.     }
  179.     endmntent(mntf);
  180.     return (NULL);            /* didn't find it */
  181. }
  182. #endif /* HAVE_QUOTACTL */
  183. #endif /* HAVE_QUOTAS */
  184.  
  185. blank ()
  186. {
  187.     if (cmcsb._cmoj)
  188.     return (cmcls ());
  189.     /* XXX shouldn't have to do it this way, but cmcsb._cmoj is
  190.        NULL when we're throwing away output inside init files, but
  191.        we want to let users clear the screen in that context */
  192.     cmcsb._cmoj = stdout;
  193.     cmcls();
  194.     cmcsb._cmoj = (FILE *) NULL;
  195. }
  196.  
  197. int
  198. cmd_blank (cmd)
  199. int cmd;
  200. {
  201.     confirm ();
  202.     blank ();
  203. }
  204.  
  205. int
  206. cmd_daytime (n)
  207. int n;
  208. {
  209.     buffer buf;
  210.  
  211.     static fdb seq_fdb_tad = { _CMTAD };
  212.  
  213.     parse (fdbchn (&seq_fdb_tad, &cfm_fdb, nil), &pv, &used);
  214.     if (used == &seq_fdb_tad) {
  215.     time_t t = datimetogmt (&pv._pvtad);
  216.     confirm ();
  217.     printf ("Local time: %s\n", daytime (t));
  218.     return;
  219.     }
  220.     printf ("%s\n", daytime ((time_t) 0));
  221. }
  222.  
  223. int
  224. cmd_echo (n)
  225. int n;
  226. {
  227.     parse_text ("string to echo", nil);
  228.     printf ("%s\n", atmbuf);
  229. }
  230.  
  231. ustrncmp(str1, str2, n)
  232. char *str1, *str2;
  233. int n;
  234. {
  235.     char s1, s2;
  236.     while(n > 0) {
  237.      s1 = islower(*str1) ? toupper(*str1) : *str1;
  238.      s2 = islower(*str2) ? toupper(*str2) : *str2;
  239.  
  240.     if (s1 == s2) {
  241.         if (s1 == '\0') return(0);
  242.         else {
  243.         str1++; str2++; n--;
  244.         }
  245.         continue;
  246.     }
  247.     if (s1 > s2) return(1);
  248.     if (s1 < s2) return(-1);
  249.     }
  250.     return(0);
  251. }
  252.  
  253. int 
  254. cmd_cd (n)
  255. int n;
  256. {
  257.     pval parseval;            /* ccmd parse return structure */
  258.     fdb *used;                /* which fdb was used */
  259.     static fdb conffdb = { _CMCFM, CM_SDH, NULL, NULL, 
  260.                    "confirm to connect to your home directory", 
  261.                    NULL, NULL};
  262.     static fdb dirfdb = { _CMFIL, FIL_DIR, 
  263.                   NULL, NULL, "directory", NULL, NULL };
  264.  
  265.     noise ("to directory");
  266.  
  267.     dirfdb._cmdef = HOME;        /* set default */
  268.     parse (fdbchn(&dirfdb,&conffdb,NULL), &parseval, &used);
  269.     if (used == &dirfdb) {
  270.     confirm();
  271.     if (chdir (parseval._pvfil[0]) != 0) {
  272.         perror ("cd");
  273.         return;
  274.     }
  275.     return;
  276.     }
  277.     /* else cd to HOME */
  278.     if (HOME == NULL) {
  279.     fprintf (stderr, "Can't find home directory.\n");
  280.     return;
  281.     }
  282.     if (chdir (HOME) != 0) {
  283.     perror ("cd");
  284.     return;
  285.     }
  286. }
  287.  
  288.  
  289. int
  290. cmd_pwd (n)
  291. int n;
  292. {
  293.   char wd[MAXPATHLEN];
  294.   char *getwd();
  295.  
  296.   confirm();
  297.   if (getwd(wd) == NULL) {
  298.     fprintf (stderr, "%s\n", wd);
  299.     return;
  300.   }
  301.   else
  302.     printf ("%s\n", wd);
  303. }
  304.  
  305.  
  306. msgcmp(a,b)
  307. message *a, *b;
  308. {
  309.     if (a->date < b->date)
  310.     return(-1);
  311.     if (a->date > b->date)
  312.     return(1);
  313.     return(0);
  314. }
  315.  
  316. #define CS (cf->sequence)
  317. #define PS (cf->prev_sequence)
  318. #define RS (cf->read_sequence)
  319.  
  320. cmd_sort(n)
  321. {
  322.     int i;
  323.  
  324.     if (!check_cf(O_RDWR))        /* precheck for file */
  325.     return;
  326.     noise ("chronologically");
  327.     confirm();
  328.     if (!check_cf(O_WRONLY))        /* check for writeable file */
  329.         return;
  330.     qsort(&cf->msgs[1], cf->count, sizeof(message), msgcmp);
  331.     clear_sequence(CS);
  332.     clear_sequence(RS);
  333.     clear_sequence(PS);
  334.     cf->flags |= MF_DIRTY;
  335.     for(i = 0; i < cf->count; i++)
  336.     cf->msgs->flags |= M_MODIFIED;
  337. }
  338.  
  339. cmd_alias (n)
  340. int n;
  341. {
  342.     confirm();
  343.     printhelp ("alias", HELP_TOP);
  344. }
  345.  
  346.  
  347. cmd_finger(n)
  348. int n;
  349. {
  350.     extern string finger_command;
  351.     static fdb cmdfdb = { _CMTXT };
  352.     char cmd[BUFSIZ];
  353.  
  354.     parse (&cmdfdb, &pv, &used);    /* line of text */
  355.     if (strlen(finger_command) == 0)
  356.     cmerr ("no finger command defined - use SET FINGER-COMMAND to define one");
  357.     strcpy(cmd, finger_command);
  358.     strcat(cmd, " ");
  359.     strcat(cmd, pv._pvstr);
  360.     shell(cmd);
  361. }
  362.  
  363.  
  364. /*
  365.  * check the messages in cf, and see if they look ok...
  366.  * something is trashing fuat's mail file.  maybe we can spot it.
  367.  */
  368.  
  369.  
  370. void
  371. debug_validate_msgvec(str)
  372. char *str;
  373. {
  374.     int i;
  375.     char *cp, *hfind();
  376.     int uhoh = 0;
  377.     extern int memdebug;
  378.  
  379.     if (cf == NULL || cf->msgs == NULL)
  380.     return;
  381.     if (memdebug) {
  382.     for (i = 1; i <= cf->count; i++) {
  383.         cp = hfind("from", cf->msgs[i].text);
  384.         if (cp == NULL) {
  385.         if (uhoh == 0)
  386.             fprintf(stderr,"%s\n",str);
  387.         fprintf(stderr,
  388.             "***** Message %d no longer has a from field!!!\n",i);
  389.         uhoh++;
  390.         }
  391.     }
  392.     if (uhoh) {
  393.         maybe_abort("Some corrupted messages");
  394.     }
  395. #ifdef MDEBUG
  396.     m_checkranges();
  397. #endif /* MDEBUG */
  398.     }
  399. }
  400.